const app = getApp();
Page({

  data: {
    allChoose: false,
    sum: 0,
    numberMax: 1,
    cartList:[]
    // numTotal:0
  },
  onLoad: function (options) {
    this.setData({
      cartList: app.globalData.cartList
    })
  },
  onShow: function () {
    this.setData({
      cartList: app.globalData.cartList
    })
    // console.log(app.globalData.cartList);
    wx.setStorageSync('cartList', app.globalData.cartList);
    this.total();
  },

  add(event) {
    console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    // this.data.cartList[index].number = this.data.cartList[index].number + 1;
    this.setData({
      cartList: app.globalData.cartList
    })
    let goodsId = this.data.cartList[index].goodsid;
    console.log('goodsid');
    console.log(goodsId)
    wx.request({
      url: `${app.globalData.baseurl}goods/goodsDetail`,
      method: 'POST',
      data: {
        goodsid: goodsId,
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            numberMax: res.data[0].goodsnum
          });
          if (this.data.cartList[index].number < this.data.numberMax) {
            this.data.cartList[index].number ++;
            app.globalData.cartList = this.data.cartList;
            wx.setStorageSync('cartList', this.data.cartList);
            this.setData({
              cartList: app.globalData.cartList
            })
            console.log(app.globalData.cartList);
            const list=wx.getStorageSync('cartList');
            console.log(list);
            console.log(this.data.cartList[index].number);
          } else {
            wx.showToast({
              title: '当前数量已经不能增加了',
              icon: 'none'
            })
          }
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
    console.log(this.data.numberMax);
    // 更新全局里和缓存里的购物车列表数据
    app.globalData.cartList = this.data.cartList;
    wx.setStorageSync('cartList', this.data.cartList);
    this.total();
  },
  reduce(event) {
    console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    if (this.data.cartList[index].number != 1) {
      this.data.cartList[index].number = this.data.cartList[index].number - 1;
    } else {
      wx.showToast({
        title: '当前数量已经不能减少了',
        icon: 'none'
      })
    }
    this.setData({
      cartList: this.data.cartList
    })
    // 更新全局里和缓存里面的购物车列表数据
    app.globalData.cartList = this.data.cartList;
    wx.setStorageSync('cartList', this.data.cartList)
    this.total();
  },
  chooseGood(event) {
    console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    this.data.cartList[index].choose = !this.data.cartList[index].choose;
    this.setData({
      cartList: this.data.cartList
    })
    // 更新全局里和缓存里面的购物车列表数据
    app.globalData.cartList = this.data.cartList;
    wx.setStorageSync('cartList', this.data.cartList);
    this.total();
  },
  toGoodDetail(event) {
    console.log(event.currentTarget.dataset.index);
    let id = event.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../../goodDetail/goodDetail?goodsId=' + id,
    })
  },
  chooseAll() {
    this.data.allChoose = !this.data.allChoose;
    this.setData({
      allChoose: this.data.allChoose
    })
    for (let index in this.data.cartList) {
      this.data.cartList[index].choose = true;
    }
    this.setData({
      cartList: this.data.cartList
    })
    // 更新全局里和缓存里面的购物车列表数据
    app.globalData.cartList = this.data.cartList;
    wx.setStorageSync('cartList', this.data.cartList);
    this.total();
  },
  // 计算合计价格
  total() {
    let sum = 0;
    let totalNumber = 0;
    let selectGoodList = [];
    for (let index in this.data.cartList) {
      if (this.data.cartList[index].choose == true) {
        selectGoodList.push(this.data.cartList[index]);
        sum = sum + this.data.cartList[index].secprice * this.data.cartList[index].number;
        totalNumber = totalNumber + this.data.cartList[index].number;
      }
    }
    this.setData({
      sum: sum.toFixed(2),
    })
    console.log(selectGoodList)
    app.globalData.numTotal = totalNumber;
    app.globalData.sumPrice = sum;
    app.globalData.orderList = selectGoodList;
  },
  // 跳入订单页面
  toOrder() {
    if (app.globalData.orderList.length == 0) {
      wx.showToast({
        icon: 'error',
        title: '请选择商品',
      })
      return
    }
    console.log(app.globalData.userInfo);
    if (app.globalData.userInfo == null) {
      wx.showToast({
        icon: 'error',
        title: '请先登录',
      })
      return
    }
    // console.log(orderList);
    wx.navigateTo({
      url: '../../order/order',
    })
  }
})